﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using Twitterizer;
using System.Globalization;

namespace TwitterPlugin
{
    public partial class frmLogin : Form
    {
        private Configuration _cfg;
        private string requestToken;
        private string buildAuthorizationUri;

        public frmLogin(Configuration config)
        {
            _cfg = config;
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            OAuthTokenResponse requestAccessTokens = OAuthUtility.GetRequestToken(
               Convert.ToString(_cfg.AppSettings.Settings["Twitterizer.Desktop.ConsumerKey"].Value),
               Convert.ToString(_cfg.AppSettings.Settings["Twitterizer.Desktop.ConsumerSecret"].Value),
               "oob");




            this.requestToken = requestAccessTokens.Token;
            this.buildAuthorizationUri = OAuthUtility.BuildAuthorizationUri(requestAccessTokens.Token).AbsoluteUri;
        }

        private void btnSignInwithTwitter_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(buildAuthorizationUri);
        }

        private void btnAuthorize_Click(object sender, EventArgs e)
        {
            try
            {
                OAuthTokenResponse accessTokens = OAuthUtility.GetAccessToken(
                        Convert.ToString(_cfg.AppSettings.Settings["Twitterizer.Desktop.ConsumerKey"].Value),
                        Convert.ToString(_cfg.AppSettings.Settings["Twitterizer.Desktop.ConsumerSecret"].Value),
                        this.requestToken,
                        this.tbxPinCode.Text);

                _cfg.AppSettings.Settings["Twitterizer.Desktop.AccessToken"].Value = accessTokens.Token;
                _cfg.AppSettings.Settings["Twitterizer.Desktop.AccessTokenSecret"].Value = accessTokens.TokenSecret;
                _cfg.AppSettings.Settings["Twitterizer.Desktop.UserId"].Value = accessTokens.UserId.ToString(CultureInfo.CurrentCulture);
                _cfg.AppSettings.Settings["Twitterizer.Desktop.ScreenName"].Value = accessTokens.ScreenName;
                if (_cfg.AppSettings.Settings.AllKeys.Contains("Twitterizer.Desktop.RememberMe"))
                    _cfg.AppSettings.Settings["Twitterizer.Desktop.RememberMe"].Value = chkRememberMe.Checked.ToString();
                else
                    _cfg.AppSettings.Settings.Add("Twitterizer.Desktop.RememberMe", chkRememberMe.Checked.ToString());
                _cfg.Save();

                ConfigurationManager.RefreshSection("appSettings");

            }
            catch (TwitterizerException tex)
            {
                MessageBox.Show(
                    string.Format("You could not be authenticated: {0}", tex.Message));
            }
        }

        private void chkRememberMe_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}
